package br.com.abella.snippets.swing;

import java.awt.AWTException;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class TakeScreenshot {

	private BufferedImage bufferedImage;
	
	/**
	 * 
	 * @param height
	 * @param weidth
	 * @return
	 */
	public BufferedImage takeScreenshot(int height, int weidth) {
		return this.takeScreenshot(new Rectangle(height,weidth));
	}
	
	/**
	 * 
	 * @param areaToBeCaptured
	 * @return
	 */
	public BufferedImage takeScreenshot(Rectangle areaToBeCaptured) { 
		
		try {
			
			Robot robot = new Robot();
			
			bufferedImage = robot.createScreenCapture(areaToBeCaptured);
	        
		} catch (AWTException e) {
			e.printStackTrace();
		}
		
		return bufferedImage;
	}
	
	/**
	 * 
	 * @param filename
	 * @param extension
	 * @throws IllegalArgumentException
	 * @throws IOException
	 */
	public void saveCapturedImageAsFile(String filename,String extension) throws IllegalArgumentException, IOException {
		
		if(filename == null || filename.trim().equals(""))
			throw new IllegalArgumentException();
		
		ImageIO.write(bufferedImage, extension, new File(filename));
	}
}
